<?php


namespace app\manage\controller;

use app\common\model\Goods AS GoodsModel;
use app\common\model\Live AS LiveModel;
use app\common\model\LiveGoods AS LiveGoodsModel;
use app\common\model\LiveGoodsSku AS LiveGoodsSkuModel;
use app\common\model\GoodSku as GoodSkuModel;

class Live extends Base
{

    /**
     * 限时活动商品
     */
    public function addgoods(){
        $id =input('id');

        $info=LiveModel::where('id',$id)->field('id,title')->find();

        //获取已经添加商城
        $goods_list=LiveGoodsModel::where('live_id',$info['id'])->field('id,live_id,goods_id,"" goods_info,sale_price,create_time')->select()->toArray();

        $this->assign('info',$info);
        $this->assign('data',$goods_list);

        return $this->fetch();
    }


    /**
     * 获取活动商品
     */
    public function getgoods(){
        $id =input('id');
        // 当前页数
        $page   = input('get.page/d', 1);
        $goods_name=input('goods_name','');

        // 分页条件
        $offset = $this->offset($page, 15);

        $info=LiveModel::where('id',$id)->field('id,title')->find();

        //start 过滤相同时间重复商品

        $str_where[]=['status','eq',1];
        $live_ids=LiveModel::where($str_where)->column('id');

        if($live_ids){
            $ac_where=[
                ['live_id','in',$live_ids]
            ];
            $goods_ids=LiveGoodsModel::where($ac_where)->column('goods_id');
            if($goods_ids){
                $where[]=[
                    ['id','not in',$goods_ids]
                ];
            }
        }

        //end
        $where[]=[
            ['is_sale','eq',1]
        ];

        if($goods_name){
            $where[]=[
                ['goods_name','like','%'.$goods_name.'%']
            ];
        }
        // 数据总数
        $count  = GoodsModel::where($where)->count();


        $list=GoodsModel::where($where)->field('id,goods_name,market_price,sale_price,goods_stock')->order('id desc')->limit($offset)->select()->toArray();


        // 模板
        $template = $this->indexTemplate();

        $this->assign('live_id',$id);
        $this->assign('goods_name',$goods_name);
        return $this->fetch($template, [
            'page'  => $page,
            'count' => $count,
            'limit' => 15,
            'list'  => $list,
        ]);
    }

    /**
     * 数据保存
     */
    public function saveGoods(){
        $live_id=input('live_id','');
        $goods_ids=input('goods_id','');

        $arr_ids=explode(',',$goods_ids);
        $info = LiveModel::field('id')->where('id',$live_id)->find();
        if(!$info){
            $data=['code'=>-1,'msg'=>'活动不存在'];
            return $data;
        }
        $a_save=[];
        foreach ($arr_ids as $v){
            $a_save[]=[
                'live_id'=>$live_id,
                'goods_id'=>$v,
            ];
        }

        $res=(new LiveGoodsModel())->saveAll($a_save);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

    /**
     * 设置活动价
     */
    public function setPrice(){
        $id=input('id','');
        $sale_price=input('sale_price','');

        $res=(new LiveGoodsModel())->where('id',$id)->update(['sale_price'=>$sale_price]);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }
    /**
     * 设置活动价
     */
    public function setGroup(){
        $id=input('id','');
        $group_num=input('group_num','');

        $res=(new LiveGoodsModel())->where('id',$id)->update(['group_num'=>$group_num]);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

    /**
     * 删除商品
     */
    public function delActGoods(){
        $id=input('id','');

        $res=(new LiveGoodsModel())->where('id',$id)->update(['delete_time'=>time()]);
        $data=['code'=>-1,'msg'=>'保存失败'];
        if($res){
            $data=['code'=>1,'msg'=>'保存成功'];
        }

        return $data;
    }

	/**
	 * 设置规格价格
	 */
	public function addsku(){

		$goods_id =input('goods_id',0);
		$live_id   = input('live_id', 0);

		$where[]=[
			['goods_id','eq',$goods_id],
		];
		$sku_list= GoodSkuModel::where($where)->field('id,goods_id,sku_name,sale_price,0 rsku_price,stock')->select()->toArray();

		$where[]=[['live_id','eq',$live_id]];
		$rs_list= LiveGoodsSkuModel::where($where)->field('id,goods_sku_id,sale_price')->select();
		foreach ($sku_list as $k=>$v){
			foreach ($rs_list as $sv){
				if($v['id']==$sv['goods_sku_id']){
					$sku_list[$k]['rsku_price']=$sv['sale_price'];
				}
			}
		}

		$this->assign('goods_id',$goods_id);
		$this->assign('live_id',$live_id);
		$this->assign('sku_list',$sku_list);

		return $this->fetch();
	}

	/**
	 * 规格价格
	 */
	public function setSkuPrice(){
		$live_id=input('live_id',0);
		$goods_id=input('goods_id',0);
		$sale_price=input('sale_price',0);
		$goods_sku_id=input('sku_id',0);

		$where=[
			['live_id','eq',$live_id],
			['goods_id','eq',$goods_id],
			['goods_sku_id','eq',$goods_sku_id],
		];
		$id=LiveGoodsSkuModel::where($where)->value('id');

		$save['goods_id']=$goods_id;
		$save['live_id']=$live_id;
		$save['sale_price']=$sale_price;
		$save['goods_sku_id']=$goods_sku_id;

		$isUpadate=false;
		if($id){
			$save['id']=$id;
			$isUpadate=true;
		}

		$res=(new LiveGoodsSkuModel())->isUpdate($isUpadate)->save($save);
		$data=['code'=>-1,'msg'=>'保存失败'];
		if($res){
			$data=['code'=>1,'msg'=>'保存成功'];
		}

		return $data;
	}

}
